package com.gzm.user.system.controller;


import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.gzm.framework.aop.annotation.Limit;
import com.gzm.framework.aop.enums.LimitType;
import com.gzm.framework.common.CommonResult;
import com.gzm.framework.redis.RedisCache;
import com.gzm.framework.security.service.TokenService;
import com.gzm.user.system.entity.bo.LoginBody;
import com.gzm.user.system.entity.vo.LoginUser;
import com.gzm.user.system.entity.vo.LoginUserVO;
import com.gzm.user.system.service.BUserService;
import com.gzm.user.system.utils.SysConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author GZM
 * @since 2021-12-27
 */
@RestController
@RequestMapping("/user/b-user")
public class BUserController {

    @Autowired
    private BUserService bUserService;
    @Autowired
    private TokenService tokenService;

    @Autowired
    private RedisCache redisCache;

    @Value("#{T(Boolean).parseBoolean('${istourist}')}")
    private Boolean istourist;

    /**
     * 获取参数是否允许游客登录
     *
     * @return 是否允许游客登录
     */
    @GetMapping
    public CommonResult<Boolean> istourist() {
        return CommonResult.success(istourist);
    }

    @GetMapping("captchaImage")
    public CommonResult<Object> captchaImage() {
        //定义图形验证码的长、宽、验证码字符数、干扰元素个数
        CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(111, 36, 4, 6);

        Snowflake snowflake = IdUtil.getSnowflake(1, 1);
        String uuid = snowflake.nextIdStr();

        redisCache.setCacheObject(SysConstant.VERIFICATION_CODE_PREFIX + uuid, captcha.getCode(), 2, TimeUnit.MINUTES);
        Map<String, Object> imgResult = new HashMap<String, Object>(4) {{
            put("img", captcha.getImageBase64());
            put("uuid", uuid);
        }};
        return CommonResult.success(imgResult);
    }

    /**
     * 用户登录
     *
     * @param loginBody 用户登录信息封装
     * @return 用户基本信息
     */
    @PostMapping("login")
    @Limit(name = "用户登录", period = 60, count = 3, limitType = LimitType.IP)
    public CommonResult<LoginUserVO> login(@RequestBody LoginBody loginBody) {
        if (StrUtil.isNotBlank(loginBody.getUuid())) {
            boolean verif = StrUtil.equals(loginBody.getCode(), redisCache.getCacheObject(SysConstant.VERIFICATION_CODE_PREFIX + loginBody.getUuid()));
            if (!verif) {
                return CommonResult.failed("验证码错误");
            }
        }
        // 生成令牌
        String token = bUserService.login(loginBody.getUsername(), loginBody.getPassword());

        LoginUser loginUser = tokenService.getLoginUser(token);

        loginUser.setToken(token);

        return CommonResult.success(bUserService.getLoginUser(loginUser));
    }

}
